/*
 * ArrayListTableModel.java
 *
 * Created on 21 de agosto de 2007, 9:46
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
//Modificado
package utiles;
import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.table.*;

/**
 *
 * @author EPSGRAU
 */
public class ArrayListTableModel extends AbstractTableModel implements Serializable {

    
    /** Creates a new instance of ArrayListTableModel */
    public ArrayListTableModel( ArrayList d, ArrayList i, ArrayList c ) {
        this.datos = d;
        this.identificadores = i;
        this.clases = c;    
        this.datosArreglo = null;
        this.columnas = null;
    }

     public ArrayListTableModel( ArrayList d, ArrayList i, ArrayList c, ArrayList e ) {
        this.datos = d;
        this.identificadores = i;
        this.clases = c;
        this.datosArreglo = null;
        this.columnas = null;
        this.editable = e;
    }
           
    public ArrayListTableModel( String[][] dat ) {
        this.datosArreglo = null;
        this.columnas = null;
        this.convertToArrayList( dat );
    }
    
    public ArrayListTableModel( String[][] dat, int[] col ) {
        this.columnas = col;
        this.datosArreglo = dat;
        this.convertToArrayList( dat );
    }
    
    public Object getValueAt( int f, int c ){
        Object x = null;
        ArrayList temp = ( ArrayList ) datos.get( f );
        x = temp.get( c );
        return x;
    }
    
    public int getColumnCount(){
        return identificadores.size();
    }

    public boolean isCellEditable(int row, int col) {
        System.out.println("editable: "+editable.get( col ));
        Boolean temp = ( Boolean ) editable.get( col );
        return temp;        
    }

    @Override
    public void setValueAt( Object val, int f, int c ){
        ArrayList temp = ( ArrayList ) datos.get( f );
        temp.set( c, val );
        this.fireTableCellUpdated( f, c );
    }   
        
    public int getRowCount(){        
        return datos.size();
    }
    
    @Override
    public String getColumnName( int i ){
        return identificadores.get( i ).toString();
    }
    
    @Override
    public Class getColumnClass( int i ){        
        return ( Class ) clases.get( i );
    }       
    
    public ArrayList getDatos(){
        return this.datos;
    }
    
    public ArrayList getIdentificadores(){
        return this.identificadores;
    }
    
    public ArrayList getClases(){
        return this.clases;
    }
    
    public String[][] getDatosArreglo(){
        return this.datosArreglo;
    }
    
    private void convertToArrayList( String[][] arreglo ){
        ArrayList temp = new ArrayList( 1 );
        ArrayList temp2 = null;        
        if(this.columnas == null ){
            for( int i = 2; i < arreglo.length; i++  ){
                temp2 = this.convertToArrayList( arreglo[ i ] );
                temp.add( temp2 );
            }
            this.identificadores = this.convertToArrayList( arreglo[ 0 ] );
            this.clases = this.convertToArrayListClases( arreglo[ 1 ] );
        }else{
            for( int i = 2; i < arreglo.length; i++  ){
                temp2 = this.convertToArrayListDeleting( arreglo[ i ] );
                temp.add( temp2 );
            }
            this.identificadores = this.convertToArrayListDeleting( arreglo[ 0 ] );
            this.clases = this.convertToArrayListClasesDeleting( arreglo[ 1 ] );
        }                
        this.datos = temp;        
    }
    
    private ArrayList convertToArrayList( String[] arreglo ){
        ArrayList temp = new ArrayList( 1 );
        for( int i = 0; i < arreglo.length; i++ ){
            temp.add( arreglo[ i ] );            
        }        
        return temp;
    }
    
    private ArrayList convertToArrayListDeleting( String[] arreglo ){
        ArrayList temp = new ArrayList( 1 );
        for( int i = 0; i < columnas.length; i++ ){            
            temp.add( arreglo[ this.columnas[i] - 1 ] );                       
        }
        return temp;
    }
            
    private ArrayList convertToArrayListClases( String[] arreglo ){
        ArrayList temp = new ArrayList( 1 );
        for( int i = 0; i < arreglo.length; i++ ){
            try{                
                temp.add( Class.forName( arreglo[ i ] ) );            
            }catch( Exception er ){
                er.printStackTrace();                
            }
        }        
        return temp;
    }  
    
    private ArrayList convertToArrayListClasesDeleting( String[] arreglo ){
        ArrayList temp = new ArrayList( 1 );
        for( int i = 0; i < columnas.length; i++ ){
            try{                
                temp.add( Class.forName( arreglo[ columnas[ i ] - 1 ] ) );            
            }catch( Exception er ){
                er.printStackTrace();                
            }
        }        
        return temp;
    }
    
    private ArrayList datos;
    private ArrayList identificadores;
    private ArrayList clases;
    private int[] columnas;
    private String[][] datosArreglo;
    private ArrayList editable;
}
